package com.scs.application.modules.upms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scs.application.core.model.CommonResult;
import com.scs.application.core.mvc.controller.BaseController;
import com.scs.application.core.model.PageResult;
import com.scs.application.modules.upms.entity.Policy;
import com.scs.application.modules.upms.request.PolicyListRequest;
import com.scs.application.modules.upms.service.PolicyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

;

/**
 * @Description PolicyController
 **/
@RestController
@RequestMapping("/upms/policies")
public class PolicyController extends BaseController {
    @Autowired
    private PolicyService policyService;

    @PostMapping
    public CommonResult add(@RequestBody Policy policy) {
        policy.setCreatedUsername("system");
        boolean bSave = policyService.save(policy);
        return bSave ? CommonResult.ok("新增成功", policy) : CommonResult.error("新增失败", policy);

    }

    @PutMapping
    public CommonResult update(@RequestBody Policy policy) {
        boolean bUpdate = policyService.updateById(policy);

        return bUpdate ? CommonResult.ok("保存成功", policy) : CommonResult.error("保存失败", policy);
    }


    @GetMapping("page")
    public PageResult page(PolicyListRequest request) {
        IPage page = policyService.page(request.getPage(), new QueryWrapper<Policy>()
                .like(!StringUtils.isEmpty(request.getPolicy_name()), "policy_Name", request.getPolicy_name()));

        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }


    @DeleteMapping("/{id}")
    public CommonResult deleteById(@PathVariable("id") String id) {
        boolean bRemove = policyService.removeById(id);
        return bRemove ? CommonResult.ok("删除成功") : CommonResult.error("删除失败");
    }
}
